package com.zyh.dao.impl;

import com.zyh.dao.PostDao;
import com.zyh.pojo.Post;
import com.zyh.service.UserService;
import com.zyh.util.DateUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;

@Repository
public class PostDaoImpl implements PostDao {
    @Autowired
    RedisTemplate<String,Object> redisTemplate;
    @Autowired
    UserService userService;
    @Override
    public void create(Post post) {
        HashMap<String,Object> map=new HashMap<>();
        map.put("userId",post.getUid());
        map.put("username",post.getUsername());
        map.put("content",post.getContent());
        map.put("time",DateUtil.DateToString(post.getDate()));
        redisTemplate.opsForHash().putAll("post:"+post.getId(),map);
        redisTemplate.opsForList().leftPush("user:"+post.getUid()+":post",post.getId());
        redisTemplate.opsForList().leftPush("timeline",post.getId());
    }

    @Override
    public Integer getPostId() {
        redisTemplate.opsForValue().increment("postId",1);
        return (Integer) redisTemplate.opsForValue().get("postId");
    }

    @SneakyThrows
    @Override
    public List<Post> findPostsByUid(Integer id) {
        List<Object> range =redisTemplate.opsForList().range("user:" + id + ":post", 0, -1);
        List<Post> posts=new ArrayList<>();
        String username=userService.findById(id).getUsername();
        for(Object postid:range){
            Integer pid=(Integer)postid;
            Post post=new Post();
            post.setUid(id);
            post.setContent((String) redisTemplate.opsForHash().get("post:" + pid, "content"));
            post.setId(pid);
            post.setUsername(username);
            post.setDate(DateUtil.StringToDate((String) redisTemplate.opsForHash().get("post:" + pid,"time")));
            posts.add(post);
        }
        return posts;
    }

    @SneakyThrows
    @Override
    public List<Post> findAll() {
        List<Object> range =redisTemplate.opsForList().range("timeline", 0, -1);
        List<Post> posts=new ArrayList<>();
        for(Object postid: range){
            Integer pid=(Integer)postid;
            Post post=new Post();
            post.setId(pid);
            post.setUsername((String) redisTemplate.opsForHash().get("post:" + pid, "username"));
            post.setUid((Integer) redisTemplate.opsForHash().get("post:" + pid, "userId"));
            post.setDate(DateUtil.StringToDate((String) redisTemplate.opsForHash().get("post:" + pid,"time")));
            post.setContent((String) redisTemplate.opsForHash().get("post:" + pid, "content"));
            posts.add(post);
        }
        return posts;
    }

    @Override
    public void add_mention(Integer pid,List<String> usernamList) {
        for(String username:usernamList)
        redisTemplate.opsForList().leftPush("user:"+username.substring(1)+":mention",pid);
    }

    @SneakyThrows
    @Override
    public List<Post> findMentionsByName(String name) {
        List<Object> range = redisTemplate.opsForList().range("user:" + name + ":mention", 0, -1);
        List<Post> postList=new ArrayList<>();
        for(Object pid:range){
            Integer id=(Integer)pid;
            Post post=new Post();
            post.setId(id);
            post.setUsername((String) redisTemplate.opsForHash().get("post:"+id,"username"));
            post.setContent((String) redisTemplate.opsForHash().get("post:"+id,"content"));
            post.setUid((Integer) redisTemplate.opsForHash().get("post:"+id,"userId"));
            post.setDate(DateUtil.StringToDate((String) redisTemplate.opsForHash().get("post:" + id,"time")));
            postList.add(post);
        }
        return postList;
    }


}
